﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Guia.Core;

namespace GuiaTest
{
    /// <summary>
    /// Summary description for ClockTest
    /// </summary>
    [TestClass]
    public class ClockTest : AbstractTest
    {

        [TestMethod]
        public void TestRun()
        {
            Action action = () =>
                                {
                                    Delay.ForMilliseconds(500);
                                };
            Action timeoutAction = () =>
                                       {
                                           throw new Guia.Core.ElementNotFoundException("timeout occured");
                                       };
            Clock.Run(action,timeoutAction,1000);
        }

        [TestMethod][ExpectedException(typeof(Guia.Core.ElementNotFoundException))]
        public void TestRunTimeout()
        {
            Action action = () =>
            {
                Delay.ForMilliseconds(2000);
            };
            Action timeoutAction = () =>
            {
                throw new Guia.Core.ElementNotFoundException("timout occured");
            };
            Clock.Run(action, timeoutAction, 1000);
        }

        [TestMethod]
        [ExpectedException(typeof(Guia.Core.ElementNotFoundException))]
        public void TestRunTimeoutWithCondition()
        {
            Action action = () =>
            {
                while(true)
                {
                    Delay.ForMilliseconds(100);
                }
            };
            Action timeoutAction = () =>
            {
                throw new Guia.Core.ElementNotFoundException("timeout occured");
            };
            Clock.Run(action, timeoutAction, 1000);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestRunActionNull()
        {
            Action timeoutAction = () =>
            {
                throw new Guia.Core.ElementNotFoundException("timeout occured");
            };
            Clock.Run(null, timeoutAction, 1000);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestRunTimeoutActionNull()
        {
            Action action = () =>
            {
                Delay.ForMilliseconds(500);
            };
            Clock.Run(action, null, 1000);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void TestRunTimeoutInvalid()
        {
            Action action = () =>
            {
                Delay.ForMilliseconds(500);
            };
            Action timeoutAction = () =>
            {
                throw new Guia.Core.ElementNotFoundException("timeout occured");
            };
            Clock.Run(action, timeoutAction, 0);
        }
    }
}
